package com.ljh.cvsspringboot.contorller;

import com.ljh.cvsspringboot.entity.SysUser;
import com.ljh.cvsspringboot.service.SysRoleService;
import com.ljh.cvsspringboot.service.SysUserService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.IOException;
import java.util.List;

/**
 * @author 27013
 **/

@Controller
public class SysUserController {
  @Autowired
  private SysUserService sysUserService;
  @Resource
  private SysRoleService sysRoleService;
    @GetMapping(value = "/sys/login")
    @ResponseBody
    public String login(HttpServletRequest request, HttpServletResponse response, @RequestParam(value = "account") String account, @RequestParam(value = "password") String password) throws IOException {
      SysUser sysUser=sysUserService.findByAccountAndPassword(account,password);
      if(sysUser!=null){
        request.getSession().setAttribute("user",sysUser);
        //登录成功，重定向到首页
        response.sendRedirect(request.getContextPath()+"/frame");
      }

        return "成功！";
    }
  @GetMapping(value = "/sys/user/list")
    public String userList(Model model){
      List<SysUser> list=sysUserService.list();
      model.addAttribute("userList",list).addAttribute("roleList",sysRoleService.list());
      return "sysUser/list";
    }
}
